/**
 * Track log
 * Created by kevin on 2018/7/27.
 */

import Track from './track'
import trackData from './data'
import constant from '../utils/constant.utils'
import './debug'

// 特殊处理上报页面
const trackPageWhiteList = ['pages/special-details', 'pages/goods-details']

const TrackObj = Object.assign(Track, {
  getTrackData(trackId) {
    let tag = Object.prototype.hasOwnProperty.call(trackData, trackId)
    if (!trackId || !tag || Object.keys(trackData[trackId]).length <= 0) {
      console.error(`${trackId}:缺少上报信息`)
    }

    this.setConfig({
      b_feature: trackData[trackId]['b_feature'],
      b_featurename: trackData[trackId]['b_featurename']
    })

    return tag
  },

  /**
   * 浏览日志上报 统一入口
   * @param trackId
   * @param bizExt
   */
  pageViewTrackBuriedPointByTrackId({
    trackId = '',
    bizExt = {},
    instance = null
  }) {
    if (!this.getTrackData(trackId)) return
    let data = trackData[trackId],
      track = { trackType: 'pageview' }

    track = Object.assign(track, {
      biz_ext: Object.assign(data.biz_ext || {}, bizExt)
    })

    this.biPushBehaviorData({
      b_title: data.b_title,
      b_link: trackId,
      trackType: track.trackType,
      bizExt: track.biz_ext,
      instance
    })
  },

  /**
   * 点击日志上报
   * @param trackId
   * @param bizExt
   * @param biz
   */
  clickTrackBuriedPointByTrackId({
    trackId = '',
    biz = {},
    bizExt = {},
    instance = null
  }) {
    if (!this.getTrackData(trackId)) return
    let data = trackData[trackId],
      track = { trackType: 'click' }

    track = Object.assign(track, {
      biz: Object.assign(data.biz || {}, biz),
      biz_ext: Object.assign(data.biz_ext || {}, bizExt)
    })

    this.mergeTrackData(instance, track, trackData)
  },

  /**
   * 分享日志上报
   * @param trackId
   * @param bizExt
   * @param biz
   */
  shareTrackBuriedPointByTrackId({
    trackId = '',
    biz = {},
    bizExt = {},
    instance = null
  }) {
    if (!this.getTrackData(trackId)) return
    let data = trackData[trackId],
      track = { trackType: 'shard' }

    track = Object.assign(track, {
      biz: Object.assign(data.biz || {}, biz),
      biz_ext: Object.assign(data.biz_ext || {}, bizExt)
    })

    this.mergeTrackData(instance, track, trackData)
  },

  /**
   *
   * @param {Object} instance
   * @param {Object} track
   * @param {Object} trackData
   */
  mergeTrackData(instance, track, trackData) {
    let pageKey = instance.$wxpage.route
    this.setConfig({
      b_feature: trackData[pageKey]['b_feature'],
      b_featurename: trackData[pageKey]['b_featurename']
    })

    this.biPushBehaviorData({
      b_title: trackData[pageKey]['b_title'],
      b_link: pageKey,
      trackType: track.trackType,
      biz: track.biz,
      bizExt: track.biz_ext,
      instance
    })
  },

  /**
   * 商品与专题详情上报商品id和专题id
   * @param {Object} instance
   * @param {Object} biz_ext
   */
  addTrackBizExtData(instance, biz_ext) {
    let { route, options } = instance.$wxpage

    if (trackPageWhiteList.includes(route)) {
      switch (route) {
        case 'pages/special-details':
          biz_ext['object_id'] = options['pageId']
          break
        case 'pages/goods-details':
          biz_ext['object_id'] = options['id']
          break
      }
    }
  },

  /**
   * 上报日志入口
   * @param {Object} params
   */
  biPushBehaviorData({
    trackType = 'pageview',
    biz = {},
    bizExt = {},
    b_title = '',
    b_link = '',
    instance = null
  } = {}) {
    let globalData = wx.$getGlobalData()
    let params = {
      b_title: b_title,
      b_link: b_link,
      biz_ext: {
        xcx_appid: globalData['appId'],
        xcx_type: constant.bizType,
        xcx_name: '专题小程序',
        uid_type: 'openid',
        uid: globalData['openId'],
        enterpriseid: globalData['enterpriseId']
      }
    }

    Object.keys(bizExt).length > 0 && Object.assign(params.biz_ext, bizExt)
    Object.keys(biz).length > 0 && Object.assign(params, { biz: biz })
    this.addTrackBizExtData(instance, params.biz_ext)
    Track.pushBehaviorData(trackType, params)
  }
})

export { TrackObj }
